#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : week_report_member.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-06-28 17:12 
@Desc    : 电话接起情况与im
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

from report_system.utils import mail_util
from report_system.utils import mailbox_util
from report_system.utils.log_util import log
from report_system.report_specify.service_specify import phone_sale_month
from report_system.report_specify.service_specify import phone_detail_month
from report_system.report_specify.service_specify import im_lodge_month
from report_system.report_specify.service_specify import im_sale_month
from report_system.report_specify.service_specify import im_detail_month
from report_system.report_specify.service_specify import im_conversion_order_sale_month
from report_system.report_specify.service_specify import im_conversion_order_lodge_month
from report_system.utils.development_util import debug

if debug:
    receivers = 'dt@iyoujia.com'
    city_mails = None
else:
    receivers = 'chenxuezi@iyoujia.com;dt@iyoujia.com;yy@iyoujia.com'
    city_mails = mailbox_util.mails_all


def process(st=None, et=None):
    # st = '2019-06-01'
    # et = '2019-06-30'
    phone_detail_month_df = phone_detail_month(st=st, et=et)
    log.info("销售月电话接起详情！")
    phone_sale_month_df = phone_sale_month(st=st, et=et)
    log.info("销售月电话接起情况！")
    im_lodge_month_df = im_lodge_month(st=st, et=et)
    log.info("房源维度月im聊单情况！")
    im_sale_month_df = im_sale_month(st=st, et=et)
    log.info("销售月im聊单情况！")
    im_detail_month_df = im_detail_month(st=st, et=et)
    log.info("销售月im聊单情况！")
    im_conversion_order_sale_month_df = im_conversion_order_sale_month(st=st, et=et)
    log.info("销售月im聊单转化详情！")
    im_conversion_order_lodge_month_df = im_conversion_order_lodge_month(st=st, et=et)
    log.info("房源月im聊单转化详情！")

    df_dict = {
        "销售月电话接起情况": phone_sale_month_df,
        "销售月电话接起详情": phone_detail_month_df,
        "销售月im聊单情况": im_sale_month_df,
        "销售im月转化率": im_conversion_order_sale_month_df,
        "房源im月转化率": im_conversion_order_lodge_month_df,
        "销售im月聊单详情": im_detail_month_df,
        "房源im月聊单情况": im_lodge_month_df,
    }

    text = """    Dear All:
                    附件是本月电话接起与im情况！
                    请查收！
                    谢谢！
            如有问题！
            请联系: dt@iyoujia.com
            """
    mail_util.distribute_mail(df_dict=df_dict, file_name='电话接起与im月度情况', text=text,
                              title="电话接起与im月度情况", receiver=receivers, na_rep='')
    # excel_util.pd_to_excel(df_dict, "im电话_001")
    # citys = im_lodge_month_df.城市.unique()
    # for city in citys:
    #     temp_dict = dict()
    #     for key in df_dict:
    #         df = df_dict[key]
    #         temp_dict[key] = df[df['城市'] == city].copy()
    #     excel_util.pd_to_excel(temp_dict, "im电话_" + str(city))


def run():
    try:
        process()
    except Exception as e:
        mail_util.error_mail("电话接起与im情况", e.__str__())
        log.exception(e)


if __name__ == '__main__':
    run()
